package com.afkt.project.ui.slice;


import com.afkt.project.base.app.BaseActivity;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseImageView;
import com.dev.base.widget.BaseTextView;
import com.dev.utils.app.ListenerUtils;
import com.dev.utils.app.SizeUtils;
import com.dev.utils.common.RandomUtils;
import com.dev.widget.ui.CornerLabelView;
import ohos.agp.components.*;

public class CornerLabelActivity extends BaseActivity implements Component.ClickedListener {

    private DirectionalLayout bar_layout;

    private DirectionalLayout bar;

    private Image bar_return;

    //12个按钮
    private BaseTextView bthColor;
    private BaseTextView bthLeft;
    private BaseTextView bthTop;
    private BaseTextView bthTriangle;
    private BaseTextView bthText1Minus;
    private BaseTextView bthText1Plus;
    private BaseTextView bthHeight1Minus;
    private BaseTextView bthHeight1Plus;
    private BaseTextView bthText2Minus;
    private BaseTextView bthText2Plus;
    private BaseTextView bthHeight2Minus;
    private BaseTextView bthHeight2Plus;
    private CornerLabelView labelView;
    private BaseImageView image3;

    private boolean mIsLeft = true;
    private boolean mIsTop = true;
    private boolean mIsTriangle = false;
    private static int count;
    private int mText1Index = 3;
    private int mText1Height = 38;
    private int convertPx = 0;
    private int mText2Index = 3;
    private int mText2Height = 18;

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_corner_label;
    }

    @Override
    protected void initView() {
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_corner_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_corner_bar, null, false);
        bar_layout.addComponent(bar);
        bar_return = (Image) bar.findComponentById(ResourceTable.Id_corner_return);

        bthColor = (BaseTextView) findComponentById(ResourceTable.Id_bth_color);
        bthLeft = (BaseTextView) findComponentById(ResourceTable.Id_bth_left);
        bthTop = (BaseTextView) findComponentById(ResourceTable.Id_bth_top);
        bthTriangle = (BaseTextView) findComponentById(ResourceTable.Id_bth_triangle);
        bthText1Minus = (BaseTextView) findComponentById(ResourceTable.Id_btn_text1_minus);
        bthText1Plus = (BaseTextView) findComponentById(ResourceTable.Id_btn_text1_plus);
        bthHeight1Minus = (BaseTextView) findComponentById(ResourceTable.Id_btn_height1_minus);
        bthHeight1Plus = (BaseTextView) findComponentById(ResourceTable.Id_btn_height1_plus);
        bthText2Minus = (BaseTextView) findComponentById(ResourceTable.Id_btn_text2_minus);
        bthText2Plus = (BaseTextView) findComponentById(ResourceTable.Id_btn_text2_plus);
        bthHeight2Minus = (BaseTextView) findComponentById(ResourceTable.Id_btn_height2_minus);
        bthHeight2Plus = (BaseTextView) findComponentById(ResourceTable.Id_btn_height2_plus);
        labelView = (CornerLabelView) findComponentById(ResourceTable.Id_vid_acl_labelview);
        image3 = (BaseImageView) findComponentById(ResourceTable.Id_corner_image3);
    }

    @Override
    protected void initListener() {
        ListenerUtils.setOnClicks(this,
                bthColor, bthLeft, bthTop, bthTriangle,
                bthText1Minus, bthText1Plus,
                bthHeight1Minus, bthHeight1Plus,
                bthText2Minus, bthText2Plus,
                bthHeight2Minus, bthHeight2Plus);

        bar_return.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(bar);
        super.onBackground();
    }

    @Override
    public void onClick(Component component) {

        String[] TEXTS = new String[]{"滿減", "赠品", "满送", "包邮", "拼图", "新人", "砍价", "预售", "众筹"};

        switch (component.getId()) {
            case ResourceTable.Id_bth_color:
                labelView.setFillColor(-0x1000000 | RandomUtils.getRandom(0, 0xffffff));
                break;
            case ResourceTable.Id_bth_left:
                if (mIsLeft) {
                    labelView.right(image3).setLeftMargin();
                } else {
                    labelView.left().setMarginLeft(0);
                }
                mIsLeft = !mIsLeft;
                break;
            case ResourceTable.Id_bth_top:
                if (mIsTop) {
                    labelView.bottom(image3).setMarginTop(252);
                } else {
                    labelView.top().setMarginTop(0);
                }
                mIsTop = !mIsTop;
                break;
            case ResourceTable.Id_bth_triangle:
                mIsTriangle = !mIsTriangle;
                labelView.triangle(mIsTriangle);
                break;
            case ResourceTable.Id_btn_text1_minus:
                mText1Index = (mText1Index - 1 + TEXTS.length) % TEXTS.length;
                labelView.setText1(TEXTS[mText1Index]);
                break;
            case ResourceTable.Id_btn_text1_plus:
                mText1Index = (mText1Index + 1) % TEXTS.length;
                labelView.setText1(TEXTS[mText1Index]);
                break;
            case ResourceTable.Id_btn_height1_minus:
                if (mText1Height < 20) return;
                mText1Height -= 4;
                convertPx = SizeUtils.dipConvertPx(mText1Height);
                labelView.setTextHeight1(convertPx);
                labelView.setPaddingTop1(convertPx);
                labelView.setPaddingCenter(convertPx / 3);
                labelView.setPaddingBottom1(convertPx / 3);
                break;
            case ResourceTable.Id_btn_height1_plus:
                if (mText1Height > 90) return;
                mText1Height += 4;
                convertPx = SizeUtils.dipConvertPx(mText1Height);
                labelView.setTextHeight1(convertPx);
                labelView.setPaddingTop1(convertPx);
                labelView.setPaddingCenter(convertPx / 3);
                labelView.setPaddingBottom1(convertPx / 3);
                break;
            case ResourceTable.Id_btn_text2_minus:
                mText2Index = (mText2Index + 5 - 1) % 5;
                labelView.setText2("1234567890".substring(0, mText2Index));
                break;
            case ResourceTable.Id_btn_text2_plus:
                mText2Index = (mText2Index + 5 + 1) % 5;
                labelView.setText2("1234567890".substring(0, mText2Index));
                break;
            case ResourceTable.Id_btn_height2_minus:
                if (mText2Height < 4) return;
                mText2Height -= 4;
                if (mText2Height + mText1Height < 108) {
                    do {
                        image3.setWidth(400 + mText2Height);
                        image3.setHeight(400 + mText2Height);
                        image3.invalidate();
                    } while (false);
                }
                convertPx = SizeUtils.dipConvertPx(mText2Height);
                labelView.setTextHeight2(convertPx);
                break;
            case ResourceTable.Id_btn_height2_plus:
                if (mText2Height > 80) return;
                if (mText2Height + mText1Height > 100) {
                    count = 400 + mText2Height;
                    image3.setWidth(count);
                    image3.setHeight(count);
                    image3.invalidate();
                }
                mText2Height += 4;
                convertPx = SizeUtils.dipConvertPx(mText2Height);
                labelView.setTextHeight2(convertPx);
                break;
        }


    }
}